<?php

require_once dirname(__FILE__) . '/../../../application/libraries/Utils.php';

/**
 * Test class for Biller.
 */
class UtilsTest extends PHPUnit_Framework_TestCase {

    /**
     * @var Biller
     */
    protected $object;

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp() {

        // Set for Money format
        setlocale(LC_MONETARY, 'en_US');

        $this->object = new Utils();
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown() {
        
    }

    /**
     * Testing is dates are the same
     */
    public function testCompare_bill_due_date1() {

        // Arrange
        $a = new Bill(1, "Leroy", 25.00, new DateTime('2005-12-06'));
        $b = new Bill(2, "Jackson", 52.00, new DateTime('2005-12-06'));

        // Act
        $actual = $this->object->compare_bill_due_date($a, $b);

        //Assert
        $this->assertEquals(0, $actual);
    }

    /**
     * Testing is date a is less then b
     */
    public function testCompare_bill_due_date2() {

        // Arrange
        $a = new Bill(1, "Leroy", 25.00, new DateTime('2005-12-06'));
        $b = new Bill(2, "Jackson", 52.00, new DateTime('2010-12-06'));

        // Act
        $actual = $this->object->compare_bill_due_date($a, $b);

        //Assert
        $this->assertEquals(-1, $actual);
    }

    /**
     * Testing is date a is greater then b
     */
    public function testCompare_bill_due_date3() {

        // Arrange
        $a = new Bill(1, "Leroy", 25.00, new DateTime('2010-12-06'));
        $b = new Bill(2, "Jackson", 52.00, new DateTime('2005-12-06'));

        // Act
        $actual = $this->object->compare_bill_due_date($a, $b);

        //Assert
        $this->assertEquals(1, $actual);
    }

    /**
     * Testing sorting of bills
     */
    public function testSort_bills() {

        // Arrange
        $list = array();
        $list[] = new Bill(1, "Leroy", 25.00, new DateTime('2010-12-06'));
        $list[] = new Bill(2, "Jackson", 52.00, new DateTime('2005-12-06'));
        $list[] = new Bill(3, "Was", 10.00, new DateTime('1974-06-22'));
        $list[] = new Bill(4, "Here", 20.00, new DateTime('1997-08-28'));

        // Act
        $this->object->sort_bills($list);

        //Assert
        $this->assertEquals(3, $list[0]->id);
        $this->assertEquals(4, $list[1]->id);
        $this->assertEquals(2, $list[2]->id);
        $this->assertEquals(1, $list[3]->id);
    }

    /**
     * Test bill creation from RRule
     */
    public function testExpand_bills1() {

        // Arrange
        $list = array();
        $start = new DateTime('2000-01-01');
        $end = new DateTime('2000-12-31');
        $id = 1;
        $name = 'Leroy Jackson';
        $amount = 25.00;
        $rrule = 'FREQ=WEEKLY;BYDAY=SU';

        // Act
        $this->object->expand_bills(&$list, $start, $end, $id, $name, $amount, $rrule);

        //Assert
        // 53 sundays in 2000
        $this->assertEquals(53, count($list));
    }

    /**
     * Test bill creation from RRule
     */
    public function testExpand_bills2() {

        // Arrange
        $list = array();
        $start = new DateTime('2000-01-01');
        $end = new DateTime('2000-12-31');
        $id = 1;
        $name = 'Leroy Jackson';
        $amount = 25.00;
        $rrule = 'FREQ=WEEKLY;BYDAY=WE';

        // Act
        $this->object->expand_bills(&$list, $start, $end, $id, $name, $amount, $rrule);

        //Assert
        // 52 Wednesdays in 2000
        $this->assertEquals(52, count($list));
    }

    /**
     * Test bill creation from RRule
     */
    public function testExpand_bills3() {

        // Arrange
        $list = array();
        $start = new DateTime('2000-01-01');
        $end = new DateTime('2000-12-31');
        $id = 1;
        $name = 'Leroy Jackson';
        $amount = 25.00;
        $rrule = 'FREQ=MONTHLY;BYMONTHDAY=25';

        // Act
        $this->object->expand_bills(&$list, $start, $end, $id, $name, $amount, $rrule);

        //Assert
        // 12 Months
        $this->assertEquals(12, count($list));
        // First is 2000-01-25
        $this->assertEquals(new DateTime('2000-01-25'), $list[0]->due_date);
        // Sixth is 2000-06-25
        $this->assertEquals(new DateTime('2000-06-25'), $list[5]->due_date);
    }

}
?>

